﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebEntities;
using WebEntities.Models.WebInfo;

namespace com.subb.WebInfo
{
    public class WI_LeaveMsgTypeDAL
    {
        public List<WI_LeaveMsgType> GetAllMsgType()
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.WI_LeaveMsgType.OrderBy(c => c.TypeID);
                List<WI_LeaveMsgType> mtypelist = query.ToList();
                return mtypelist;
            }
        }

        public WI_LeaveMsgType GetMsgType(int typeId)
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                WI_LeaveMsgType msgtype = ctx.WI_LeaveMsgType.SingleOrDefault(c => c.TypeID == typeId);
                return msgtype;
            }
        }

        public void CreateMsgType(string mtypeName)
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                WI_LeaveMsgType wimtype = new WI_LeaveMsgType
                {
                    MsgType = mtypeName
                };
                ctx.WI_LeaveMsgType.AddObject(wimtype);
                ctx.SaveChanges();
            }
        }

        public bool UpdateMsgType(int typeId, string mtypeName)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                WI_LeaveMsgType wimtype = ctx.WI_LeaveMsgType.SingleOrDefault(c => c.TypeID == typeId);
                if (wimtype != null)
                {
                    wimtype.MsgType = mtypeName;
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }

        public bool RemoveMsgType(int typeId)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                WI_LeaveMsgType wimtype = ctx.WI_LeaveMsgType.SingleOrDefault(c => c.TypeID == typeId);
                if (wimtype != null)
                {
                    ctx.WI_LeaveMsgType.DeleteObject(wimtype);
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }
    }
}
